package com.smartguard.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.CopyOnWriteArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.smartguard.bean.Device;
import com.smartguard.common.Const;

/**
 * 自定义Action的基类，包含了getSession(),getRequest()，getResponse()，getPrintWriter()方法； 
 * @author zuhuang.chen
 *
 */
public class BaseAction extends ActionSupport {
	/*public BaseAction(){
		System.out.println(this.getApplication().get(Const.SESSION_USER_DEVICE));
	}*/
	
	public Map<String, Object> getSession(){
		return ActionContext.getContext().getSession();
	}
	
	public Map<String, Object> getApplication(){
		return ActionContext.getContext().getApplication();
	}
	
	public HttpServletRequest getRequest(){
		return ServletActionContext.getRequest();
	}
	
	public HttpServletResponse getResponse(){
		return ServletActionContext.getResponse();
	}
	
	public PrintWriter getWriter() throws IOException{
		this.getResponse().setCharacterEncoding("UTF-8");
		return this.getResponse().getWriter();
	}
	
	/**
	 * 根据用户名查找该用户当前在线设备列表
	 * @param username
	 * @return
	 */
	public CopyOnWriteArrayList<Device> getUserOnlineDevice(String username){
		ConcurrentMap<String, CopyOnWriteArrayList<Device>> map = 
		(ConcurrentMap<String, CopyOnWriteArrayList<Device>>) this.getApplication().get(Const.SESSION_USER_DEVICE);
		
		CopyOnWriteArrayList<Device> onlineDevice = map.get(username);
		return onlineDevice;
	}
}
